private var scrollPosition = Vector2.zero;
private var entries = ArrayList();
public var nick = "Nick";
private var msg = "Mensagem";
private var msg2="Mensagem";
private var status = "";
private var startTime = Time.time;

class Entry
{
	var msg = "";
	var sender = "";
}

function Awake() {
	startTime = Time.time;
	Application.runInBackground = true;
}

function OnGUI () {
	nick = GUI.TextField(Rect(0,100,100,25),nick,25);
	rectWin = GUI.Window(1,Rect (0,130,300,500),WinChat,"Chat");
	msg2 = GUI.TextField(Rect(0,640,240,25),msg,25);
	if (GUI.Button(Rect(250,640,50,25),"Send")) {
		
		networkView.RPC("sendMessage",RPCMode.All,msg2);
	}
	GUI.Label(Rect(0,670,240,25),status);
	
	//var e: Event = Event.current;
	//if (e.isKey) {
	if (msg2 != msg) {
		networkView.RPC("mudaStatus",RPCMode.Others,nick+" está digitando");
		msg = msg2;
	}
}

function WinChat(id : int) {
	scrollPosition = GUILayout.BeginScrollView(scrollPosition);
	for (var entry : Entry in entries) {
		//GUILayout.BeginVertical();
		var m = entry.sender + ": " + entry.msg;
		GUILayout.Label(m);
		GUILayout.FlexibleSpace();
		//GUILayout.EndVertical();
	}
	GUILayout.EndScrollView();
}

@RPC
function sendMessage(str:String){
	var entry:Entry = new Entry();
	entry.sender = nick;
	entry.msg = str;
	entries.Add(entry);
}

@RPC
function mudaStatus(msg: String) {
	
	status = msg;
	startTime = Time.time;
}

function Update () {
	var time = Time.time;
	//if (time - startTime > 3) {
		status = "";
	//}
}
